-- core
-- -- -- https://www.jianshu.com/p/9fa3dae786b4
-- -- -- table 内部是 方法
local w, h = getScreenSize()

local UiCore = {
  default = {
    width = w,
    height = h,
    -- size
    LabelSize_large = 16,
    LabelSize_des = 14,
    LabelSize_little = 12,

    -- height
    line_height = 1,
    -- width
    line_largeWidth = 600,
    line_littleWidth = 250,

    -- color
    label_color = '230,50,92',
    label_blueColor = '0,0,255', -- 蓝色
    label_greenColor = '80,187,50', -- 绿色
    line_color = '255,160,122',
    Line_greyColor = '60,179,113',
  },
  allExplist = '',
  idkv = {},
}

----------------------------
-- 私有方法
function _UiColorSaveIdList(id,isTab,list,isMutil, isCombSubData,data, rootId)
  isTab = isTab or false
  list = list or {}
  isMutil = isMutil or false
  isCombSubData = isCombSubData or false
  data = data or ''
  rootId = rootId or ''

  local value = nil
  if isTab then
    local splitChar = ','
    value = UtilStringSplit(list, splitChar)
  else
    value = list
  end
  UiCore.idkv[id] = {
    list = value,
    isTab = isTab,
    isMutil = isMutil,
    isCombSubData = isCombSubData,
    data = data,
    rootId = rootId
  }
end

function _UiCoreUpDateExplist(id, list)
  --不在使用
  if true then return '' end
  local Explist = UiCore.allExplist
  -- UiVaPrintLog('upDateExplist id ='..tostring(id)..'--- list='..tostring(list))

  if list and type(list) == 'string' and RxString:len(list) > 0 then
    local stab = UtilStringSplit(list,',')
    local str = ''
    for k,v in pairs(stab) do
      if k ~= 1 then
        str = str .. ','
      end
      str = str .. id .. tostring(k)
    end
    id = str
  end

  if id and id ~= '' then
    if Explist and Explist ~= '' then
      Explist = Explist .. ','
    end
    Explist = Explist .. id
  end
  UiCore.allExplist = Explist
end
----------------------------

function UiCore:addUI(page,uis)
  -- UiVaPrintLog('0-0- page='..tostring(page)..'---uis='..tostring(uis))

  if not uis then
    -- UiVaPrintLog('~~ error UiCore:addUI uis=nil')
    if page then
      return page
    end
    return
  end

  if type(uis) ~= 'table' then
    -- UiVaPrintLog('1-2-3-3-4-5-6')
    if page then
      return page
    end
    return
  end

  page = page or {}
  local index = RxTable:len(page)
  -- UiVaPrintLog('UiCore:addUI  page len='..tostring(index))
  local exIndex = RxTable:len(Explist)
  for k,v in pairs(uis) do
    -- UiVaPrintLog('UiCore:addUI k-type='..tostring(type(k))..'---k='..tostring(k)..' -- v-type='..tostring(type(v)..'-- v='..tostring(v))..'--- index='..tostring(index))
    if v and v ~= '' and v ~= nil then
      index = index + 1
      local txt = v
      if type(v) == 'function' then
        txt = v()
      end
      -- UiVaPrintLog('UiCore:addUI v='..tostring(v)..'---index='..tostring(index)..'--txt='..tostring(txt))
      -- page - push
      if txt then
        page[index] = txt
      end
    end
  end

  return page
end

----------------------------

function UiCore:UITable(configName,tabTitles)
  configName = configName or 'rxsgs_paosanguo_unknow'
  tabTitles = tabTitles or '单号,敦煌设置,日常任务,活动,注意事项'
  return {
    config = configName,
    timer = '1000',
    style = 'default',
    -- rettype : 如果使用 table 构建界面，有返回值的控件如单选组合、多选组合、单行输入框等可直接指定控件 ID，相当于指定变量名称，直接将控件返回值与 ID 进行对比即可
    rettype = 'table',
    width = w, height = h,
    -- orient 指定 UI 界面显示方向，0 - Home 键在设备下方，1 - Home 键在设备右侧，2 - Home 键在设备左侧
    orient = 1,
    bgcolor = '255,255,255', -- 界面背景色
    btnbkcolor = '255,255,255', --底部按钮背景色
    -- config 配置保存文件名，保存配置到该文件
 -------------------------------------------------------
    -- pagetype 多页模式，当指定该属性值为 multi 时，UI 可分页显示
    pagetype = 'multi',
    -- 多页模式下指定默认停留的页面, 默认 '1'
    selpage = '1',
    -- pagenumtype 页码样式，dot - 小圆点， number - 数字， default - 不显示
    -- --          tab - 底部不显示页码样式，在顶部将分页标题显示为二级标签
    pagenumtype = 'tab',
    -- titles 多页标题，用于指定 UI 顶部标题栏文字在多页模式下
    --  可以设置多个标题对应多个页面，以逗号分隔
    titles = tabTitles,
    -- title 对齐方式，默认左对齐。
    -- --    可取值为：左对齐 left，右对齐 right，居中 center，
    titlealign = 'left',
    titlesize = '13',
 -------------------------------------------------------
    pages = {},
 -------------------------------------------------------
  }
end

function UiCore:pageDocoment()
  local si = sysInit or {}
  local ratio = si.ratio or {}
  local siWidth = ratio.width or 540
  local siHight = ratio.height or 960
  local tip = '★ 分辨率、尺寸: '..tostring(siWidth)..'x'..tostring(siHight)

  return UiCore.addUI(nil, {
    UiCore:Label_large('使用须知'),
    UiCore:Label(tip),
    UiCore:Label('★ 进入游戏后再 [运行] 脚本'),
    UiCore:Label('★ 进入游戏采用横屏，不支持竖屏'),
    UiCore:Label('★ 不允许[脚本运行中]切换其他应用'),
    UiCore:Label('★ 不要手动干预脚本运行'),
    UiCore:Label('★ 最好在 [pc] 上使用该脚本, 太吃内存'),
    UiCore:Line_max_width(),
    UiCore:Label_large('游戏'),
    UiCore:Label('★ 敦煌: 如果[账号]没有跑过图，请手动跑图`给100玉璧`'),
    UiCore:Label('★ 桃源居: 如果[app]再当前设备是第一次运行，请手动[打开] 神树-桃源居，根据提示完成操作'),
    UiCore:Label('★ 挑战 (敦煌-探险者): 如果[app]在当前设备是第一次运行，请手动挑战一下，根据提示 长按某个武将查看信息'),
    UiCore:Line_max_width(),
    UiCore:Label_large('安装插件 - 必须'),
    UiCore:Label("★ 敦煌-图片识别插件: 'tsimg.so' : [触动精灵 app]-> [底部导航-我的账号] -> [文件管理] -> [插件] - [官方插件]"),
    UiCore:Label("★ 敦煌-文字识别插件: 'chi_sim'  , [触动精灵 app]-> [底部导航-我的账号] -> [文件管理] -> [插件] - [官方插件]"),
  })
end

-------------
function UiCore:Label(text,co,size,align, width, nowrap)
  -- UiVaPrintLog('UiCore:Label text='..tostring(text))
  text = text or ''
  co = co or UiCore.default.label_color
  size = size or UiCore.default.LabelSize_des
  align = align or 'left'

  if width == nil then
    width = 0
  end

  nowrap = nowrap or 0

  return {
    type = 'Label',
    color = co,
    size = size,
    align = align,
    text = text,
    width = width,
    nowrap = nowrap,
    export_explist = 'unknow',
  }
end

function UiCore:LabelColumns(text, nowrap)
  align = align or 'left'
  return UiCore:Label(
    text,
    nil, nil, nil,
    -1, nowrap)
end

function UiCore:Label_little(text,align, width, nowrap)
  return UiCore:Label(
    text,
    UiCore.default.label_greenColor,
    UiCore.default.LabelSize_little,
    align, width, nowrap)
end

function UiCore:Label_little_Columns(text, nowrap)
  return UiCore:Label(
    text,
    UiCore.default.label_greenColor,
    UiCore.default.LabelSize_little,
    nil, -1, nowrap)
end

function UiCore:Label_card_Columns(text, nowrap)
  return UiCore:Label(
    text,
    UiCore.default.label_blueColor,
    UiCore.default.LabelSize_little,
    nil, -1, nowrap)
end

function UiCore:Label_large(text,align, width, nowrap)
  align = align or 'center'
  return UiCore:Label(
    text,
    nil,
    UiCore.default.LabelSize_large,
    align, width, nowrap)
end

function UiCore:Line(color,align,width,height)
  color = color or UiCore.default.Line_greyColor
  height = height or UiCore.default.line_height
  width = width or ''
  align = align or 'left'

  return {
    type = 'Line',
    color = color,
    size = size,
    align = align,
    export_explist = 'unknow',
  }
end

function UiCore:Line_min_width(color)
  color = color or UiCore.default.line_color
  return UiCore:Line(color, nil, 250, 1)
end

function UiCore:Line_max_width(color)
  color = color or UiCore.default.line_color
  return UiCore:Line(color, 'center', 600, 1)
end

function UiCore:Edit(id, prompt, kbtype, text, width, nowrap, size, secure)
  id = id or ''
  prompt = prompt or '' -- 提示
  kbtype = kbtype or 'number' -- number/ascii/password
  text = text or ''
  if width == nil then
    width = 0
  end
  if width > 1 then
    width = math.floor(w / width)
  end

  nowrap = nowrap or 0
  size = size or 'm'  -- s 小, m 中, l 大
  secure = secure or false
  _UiCoreUpDateExplist(id)
  _UiColorSaveIdList(id)
  return {
    type = 'Edit',
    id = id,
    prompt = prompt,
    text = text,
    size = size,
    kbtype = kbtype,
    width = width,
    nowrap = nowrap,
    secure = secure,
    export_explist = id
  }
end

-- on = 'off'，'on'
-- size = s 小, m 中, l 大
function UiCore:Switch(id, on, size, align, width, nowrap)
  -- UiVaPrintLog('UiCore:Switch id='..tostring(id)..' on='..tostring(on))
  id = id or ''
  on = on or 'off' -- on 开， off 关
  size = size or 'm'  -- s 小, m 中, l 大
  align = align or 'left'
  if width == nil then
    width = 0
  end
  if width > 1 then
    width = math.floor(w / width)
  end

  nowrap = nowrap or 0
  -- UiVaPrintLog('UiCore:Switch--2-22-2-2 id='..tostring(id)..' on='..tostring(on))
  _UiCoreUpDateExplist(id)
  _UiColorSaveIdList(id)
  return {
    type = 'Switch',
    id = id,
    state = on,
    size = size,
    align = align,
    width = width,
    nowrap = nowrap,
    export_explist = id
  }
end

-- 单选组合
function UiCore:RadioGroup(id, list, select, countperline, width)
  id = id or ''
  list = list or ''
  select = select or '0'
  countperline = countperline or 1 -- 默认一行
  if not width then
    width = -1
  end
  UiVaPrintLog('UiCore:RadioGroup id='..tostring(id)..'--width='..tostring(width)..'--width='..tostring(width))
  _UiCoreUpDateExplist(id)
  _UiColorSaveIdList(id,true,list)
  return {
    type = 'RadioGroup',
    id = id,
    list = list,
    select = select,
    countperline = countperline,
    width = width,
    export_explist = id
  }
end

-- 单选下拉框: select 默认 '0'；只允许填写一个，序号从 0 开始
function UiCore:ComboBox(id,list,select,data,source,width,prompt,nowrap)
  id = id or ''
  list = list or ''
  select = select or '0'
  data = data or ''
  source = source or ''
  prompt = prompt or false
  nowrap = nowrap or '0' -- 默认不换行

  if width == nil then
    width = 0
  end

  _UiCoreUpDateExplist(id)
  _UiColorSaveIdList(id,true,list, false, false, data, rootId)
  return {
    type = 'ComboBox',
    id = id,
    list = list,
    select = select,
    data = data,
    width = width,
    source = source,
    prompt = prompt,
    nowrap = nowrap,
    export_explist = id,
  }
end

function UiCore:ComboBoxSub(rootId, id, dataSource,select, prompt)
  id = id or ''
  select = select or '0'
  dataSource = dataSource or ''
  prompt = prompt or false
  rootId = rootId or ''

  _UiCoreUpDateExplist(id)
  _UiColorSaveIdList(id,false,dataSource, false, true, dataSource, rootId)
  return {
    type = 'ComboBox',
    rootId = rootId,
    id = id,
    select = select,
    dataSource = dataSource,
    prompt = prompt,
    export_explist = id,
  }
end

function UiCore:ComboBoxColumns(id,list,select, width, nowrap)
  return UiCore:ComboBox(id,list, select, nil, nil, width, nowrap)
end


-- 多选下拉框: select 默认 '0'；填写多个时以 @ 分割。序号从 0 开始
function UiCore:CheckBoxGroup(id,list,select, countperline, width, nowrap, images)
  id = id or ''
  list = list or '' -- 下拉文字
  select = select or ''
  countperline = countperline or 1
  width = width or -1

  -- 自动分割 -- 0 和 1 没有区别
  local newWidth = width
  if width > 1 then
    newWidth = math.floor(w / width)
    -- newWidth = -1
  end
  UiVaPrintLog('UiCore:CheckBoxGroup id='..tostring(id)..'--width='..tostring(width)..'--newWidth='..tostring(newWidth))
  nowrap = nowrap or '0' -- 默认不换行
  images = images or '' -- 下拉图片
  _UiCoreUpDateExplist(id, list)
  _UiColorSaveIdList(id,true,list, true)
  return {
    type = 'CheckBoxGroup',
    id = id,
    size = size,
    list = list,
    select = select,
    countperline = countperline,
    width = newWidth,
    nowrap = nowrap,
    export_explist = id
  }
end

return UiCore